package app.service;

import app.bean.Column;
import app.bean.Table;
import app.util.CommonUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import java.util.LinkedList;
import java.util.List;
import java.util.Map;

@Service
public class MySqlService {

    private Logger logger = LoggerFactory.getLogger(MySqlService.class);

    @Autowired
    JdbcTemplate jdbcTemplate;

    String convertToJava(String columnType){
        if(columnType.startsWith("INT")
                ||columnType.startsWith("TINYINT")){
            return "Integer";
        }
        if(columnType.startsWith("BIGINT")){
            return "Long";
        }
        if(columnType.startsWith("FLOAT")){
            return "Float";
        }
        if(columnType.startsWith("DOUBLE")){
            return "Double";
        }
        return "String";
    }

    public List<Table> getTables(String dbname){
        StringBuffer sql = new StringBuffer("SELECT a.table_name,a.table_comment FROM information_schema.TABLES a");
        sql.append(" WHERE a.table_schema = '"+dbname+"' ORDER BY a.table_name");
        List<Map<String, Object>> sqlToMap = jdbcTemplate.queryForList(sql.toString());
        List<Table> tables = new LinkedList<>();
        for (Map map : sqlToMap) {
            String name = CommonUtil.mapVal(map,"table_name");
            Table table = new Table();
            table.setName(name);
            table.setClassName(CommonUtil.classToHump(table.getName()));
            table.setHumpName(CommonUtil.underlineToHump(table.getName()));
            String comment = table.getName();
            String comment2 = CommonUtil.mapVal(map,"table_comment");
            if(!"".equals(comment2)){
                comment = comment2;
            }
            table.setCname(comment);
            tables.add(table);
        }
        return tables;
    }

    public String dbType(Map<String, Object> param,String key){
        if(param == null || param.size() == 0){
            return "varchar";
        }
        if(param.containsKey(key)){
            return CommonUtil.mapVal(param,key);
        }
        return "varchar";
    }

    public String javaType(String dbType){
        if(dbType == null){
            return "String";
        }
        return convertToJava(dbType.toUpperCase());
    }

    public Table getColumns(String dbname,Table table){
        StringBuffer sql = new StringBuffer("select column_name,column_comment,column_type,column_key from information_schema.columns");
        sql.append(" where table_schema = '"+dbname+"' and table_name = '"+table.getName()+"' ");
        List<Map<String, Object>> sqlToMap = jdbcTemplate.queryForList(sql.toString());
        List<Column> columns = new LinkedList<Column>();
        for (Map map : sqlToMap) {
            String name = CommonUtil.mapVal(map,"column_name");
            Column column = new Column();
            column.setName(name);
            column.setFieldName(CommonUtil.classToHump(column.getName()));
            column.setHumpName(CommonUtil.underlineToHump(column.getName()));
            column.setPk(false);
            if("PRI".equals(CommonUtil.mapVal(map,"column_key"))){
                column.setPk(true);
            }
            if("".equals(CommonUtil.mapVal(map,"column_comment"))){
                column.setCname(column.getName());
            }else{
                column.setCname(CommonUtil.mapVal(map,"column_comment"));
            }
            column.setDbType(dbType(map,"column_type"));
            column.setJavaType(javaType(column.getDbType()));

            columns.add(column);
        }
        table.setColumns(columns);
        return table;
    }

}
